#!/bin/sh /etc/rc.common

START=99

USE_PROCD=1

CRON_FILE=/etc/crontabs/root

service_triggers() {
	procd_add_reload_trigger "cloudflarespeedtest"
}

add_cron(){
	local enabled
	config_get_bool enabled $1 enabled
	
	del_cron
	
	if	[ $enabled = 1 ] ; then 
		local minute
		local hour
		local custome_cors_enabled
		local custome_cron

		config_get minute $1 minute
		config_get hour $1 hour
		config_get_bool custome_cors_enabled $1 custome_cors_enabled
		config_get custome_cron $1 custome_cron

		if [ $custome_cors_enabled = 1 ] ; then 
 			echo "$custome_cron /usr/bin/cloudflarespeedtest/cloudflarespeedtest.sh start" >> $CRON_FILE
		else
			echo "$minute $hour * * * /usr/bin/cloudflarespeedtest/cloudflarespeedtest.sh start" >> $CRON_FILE
		fi
		crontab $CRON_FILE	 
	fi
}

del_cron(){	
	sed -i '/cloudflarespeedtest/d' $CRON_FILE
	/etc/init.d/cron restart
}

stop_instance(){
	del_cron
}

stop_service() {
	config_load cloudflarespeedtest
	config_foreach stop_instance global
}
start_service() {
	config_load cloudflarespeedtest
	config_foreach add_cron global	 
}
reload_service() {
	stop
	start
}